package com.ruike.alisurface.ui.mainPage;

import android.view.View;
import android.widget.TextView;

import com.ruike.alisurface.R;
import com.ruike.alisurface.Serials.MsgEventBus;
import com.ruike.alisurface.answerQuestion.AnswerQuestionActivity;
import com.ruike.alisurface.answerQuestion.QrCodeActivity;
import com.ruike.alisurface.answerQuestion.http.ZhuangYanHttp;
import com.ruike.alisurface.base.BaseAty;
import com.ruike.alisurface.bean.TimeLimitBean;
import com.ruike.alisurface.utils.ShareKey;
import com.ruike.alisurface.utils.TimeUtils;
import com.voodoo.lib_utils.GsonUtils;
import com.voodoo.lib_utils.L;
import com.voodoo.lib_utils.ShareUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

import static com.ruike.alisurface.answerQuestion.http.ZhuangYanHttp.GET_IN_TIME_LIMIT_URL;
import static com.ruike.alisurface.answerQuestion.http.ZhuangYanHttp.GET_TIME_LIMIT_URL;

public class SelectTypeMainActivity extends BaseAty {

    //    @BindView(R.id.selectTypeMain_payTimePeriod_tv)
//    TextView payTimePeriodTv;
    @BindView(R.id.selectTypeMain_answerTimePeriod_tv)
    TextView answerTimePeriodTv;

    int clickType = 0; // 1为购买按钮点击  2为答题按钮点击

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_select_type_main;
    }

    @Override
    protected void initViews() {
        initTitleBar(true);
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void onStart() {
        super.onStart();
        showProgressDialog("正在获取机器相关设置");
        ZhuangYanHttp.getTimeLimit(this);
    }

    @Override
    public void doEventBusResult(MsgEventBus event) {
    }

    @OnClick({R.id.selectTypeMain_toPay_btn, R.id.selectTypeMain_toAnswerQuestion_btn})
    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.selectTypeMain_toPay_btn:
                clickType = 1;
                if (ShareUtils.getInstance().getInt(ShareKey.IS_SHOP_TYPE, 1) == 1) {
                    startActivity(MainActivity.class, null); // 按货道展示商品
                } else {
                    startActivity(MainTypeActivity.class, null); // 按货商品类型示商品
                }
                break;
            case R.id.selectTypeMain_toAnswerQuestion_btn:
                clickType = 2;
                showProgressDialog("正在验证相关信息...");
                ZhuangYanHttp.getInTimeLimit(clickType, this);
                break;
        }
    }

    @Override
    public void onSuccess(String requestUrl, String requestJsonStr) {
        super.onSuccess(requestUrl, requestJsonStr);
        // 获取到的时间限制以及答题展示时间等
        if (GET_TIME_LIMIT_URL.equals(requestUrl)) {
            TimeLimitBean timeLimitBean = null;
            try {
                JSONObject jsonObject = new JSONObject(requestJsonStr);
                JSONObject jsonData = jsonObject.optJSONObject("data");
                timeLimitBean = GsonUtils.json2Model(jsonData != null ? jsonData.toString() : "{}", TimeLimitBean.class);
            } catch (JSONException e) {
                L.e("获取时间段接口返回JSON数据格式异常：" + requestJsonStr);
                return;
            }
//            TimeLimitBean.SalesTimeSetBean salesTimeSetBean = timeLimitBean.getSales_time_set();
//            payTimePeriodTv.setText(salesTimeSetBean == null ? "获取限制时间异常" : String.format("购买时间段：%s ~ %s", salesTimeSetBean.getStart_time(), salesTimeSetBean.getEnd_time()));
            TimeLimitBean.QuestionTimeSetBean questionTimeSetBean = timeLimitBean.getQuestion_time_set();
            answerTimePeriodTv.setText(questionTimeSetBean == null ? "获取限制时间异常" : String.format("答题时间段：%s ~ %s", questionTimeSetBean.getStart_time(), questionTimeSetBean.getEnd_time()));

            int questionTime = timeLimitBean.getQuestionTime();
            int errorWaitTime = timeLimitBean.getErrorWaitTime();
            ShareUtils.getInstance().putInt(ShareKey.QUESTION_SHOW_TIME_KEY, questionTime); // 题目倒计时时长
            ShareUtils.getInstance().putInt(ShareKey.ANSWER_SHOW_TIME_KEY, errorWaitTime); // 回答错误展示正确答案时长
//            ShareUtils.getInstance().putBoolean(ShareKey.IS_SHOW_QUESTION_ANSWER_KEY, false); // 保存是否展示正确答案
        }
        // 检查是否在时间段内
        if (GET_IN_TIME_LIMIT_URL.equals(requestUrl)) {
            if (clickType == 1) {
                if (ShareUtils.getInstance().getInt(ShareKey.IS_SHOP_TYPE, 1) == 1) {
                    startActivity(MainActivity.class, null); // 按货道展示商品
                } else {
                    startActivity(MainTypeActivity.class, null); // 按货商品类型示商品
                }
            } else if (clickType == 2) {
                startActivity(QrCodeActivity.class, null);
            }
        }
    }

    @Override
    public void onError(String requestUrl, String errorMsg) {
        super.onError(requestUrl, errorMsg);
        // 获取到的时间限制以及答题展示时间等
        if (GET_TIME_LIMIT_URL.equals(requestUrl)) {
            showErrorTip(errorMsg);
        }
        // 检查是否在时间段内
        if (GET_TIME_LIMIT_URL.equals(requestUrl)) {
            showErrorTip("根据服务器时间，当前不在" + (clickType == 1 ? "购买" : clickType == 2 ? "答题" : "") + "时间段内");
        }
    }

}
